package com.lishu.carsafe.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lishu.carsafe.R;
import com.lishu.carsafe.adapter.TypeListAdapter;
import com.lishu.carsafe.model.entity.Type;

import java.util.List;

public class ChooseDialog extends AlertDialog {
    private TextView dialogTitle;
    private ListView optionsList;
    private TypeListAdapter optionsListAdapter;
    private AlertDialog alertDialog;
    private Context mContext;

    public ChooseDialog(Context context) {
        super(context);

        mContext = context;
        initDialog();
    }

    private void initDialog(){
        alertDialog = new AlertDialog.Builder(mContext).create();
        alertDialog.show();
        Window window = alertDialog.getWindow();
        window.setGravity(Gravity.CENTER_HORIZONTAL);
        window.setContentView(R.layout.layout_choose_dialog);

        dialogTitle = (TextView) window.findViewById(R.id.dialog_title);
        /*ImageView closeDialog = (ImageView) window.findViewById(R.id.close);
        closeDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });*/
        optionsList = (ListView) window.findViewById(R.id.options_list);
        optionsListAdapter = new TypeListAdapter(mContext);
        optionsList.setAdapter(optionsListAdapter);
    }

    public void setDialogTitle(String title){
        dialogTitle.setText(title);
    }

    public void setDialogData(List<Type> list){
        optionsListAdapter.setData(list);
    }

    public void setDialogSeclection(int position){
        optionsListAdapter.setSeclection(position);
    }

    public void setItemClickListener(AdapterView.OnItemClickListener listener){
        optionsList.setOnItemClickListener(listener);
    }

    public void dismissDialog(){
        alertDialog.dismiss();
    }
}
